import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/LoginView.vue'),
    meta: { requiresAuth: false, title: '用户登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/RegisterView.vue'),
    meta: { requiresAuth: false, title: '用户注册' }
  },
  {
    path: '/',
    component: () => import('@/layouts/DefaultLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      { 
        path: '', 
        name: 'Home', 
        component: () => import('@/views/HomeView.vue'),
        meta: { title: '首页' }
      },
      { 
        path: 'projects', 
        name: 'Projects', 
        component: () => import('@/views/ProjectListView.vue'),
        meta: { title: '课题库' }
      },
      { 
        path: 'publish', 
        name: 'Publish', 
        component: () => import('@/views/EnterprisePublishView.vue'),
        meta: { title: '发布课题', roles: ['enterprise'] }
      },
      { 
        path: 'applications', 
        name: 'Applications', 
        component: () => import('@/views/ApplicationListView.vue'),
        meta: { title: '我的申请', roles: ['supervisor', 'student'] }
      },
      { 
        path: 'admin', 
        name: 'AdminMatch', 
        component: () => import('@/views/AdminMatchView.vue'),
        meta: { title: '匹配管理', roles: ['admin'] }
      },
      { 
        path: 'statistics', 
        name: 'Statistics', 
        component: () => import('@/views/StatisticsView.vue'),
        meta: { title: '数据统计', roles: ['admin'] }
      },
      { 
        path: 'about', 
        name: 'About', 
        component: () => import('@/views/AboutView.vue'),
        meta: { title: '关于平台' }
      },
      { 
        path: 'form-demo', 
        name: 'FormDemo', 
        component: () => import('@/views/FormDemoView.vue'),
        meta: { title: '表单演示' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFoundView.vue'),
    meta: { requiresAuth: false, title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 中山大学研究生院校企对接平台`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth === false) {
    // 登录和注册页面，如果已登录则跳转到首页
    if (userStore.isLoggedIn && (to.path === '/login' || to.path === '/register')) {
      next('/')
      return
    }
    next()
    return
  }
  
  // 需要认证的页面
  if (!userStore.isLoggedIn) {
    // 未登录，跳转到登录页
    next('/login')
    return
  }
  
  // 检查角色权限
  if (to.meta.roles && Array.isArray(to.meta.roles) && to.meta.roles.length > 0) {
    const userRole = userStore.userRole
    if (!userRole || !to.meta.roles.includes(userRole)) {
      // 角色不匹配，跳转到首页
      ElMessage.error('您没有权限访问该页面')
      next('/')
      return
    }
  }
  
  next()
})

export default router
